package mortal.game.control
{
    import Framework.Util.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import flash.utils.*;
    import modules.*;
    import mortal.common.error.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.shortcuts.item.*;
    import mortal.game.view.system.*;
    import mortal.game.view.trade.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TradeController extends Controller
    {
        private var _tradeModule:TradeModule;
        private var _tradeHintIcon:TradeHintIcon;
        private var _requestList:Vector.<SBusiness>;
        private var requestFromEntityId:int;
        public var currentBusinessId:String = "";
        private var _reqList:Vector.<SBusiness>;

        public function TradeController()
        {
            this._requestList = new Vector.<SBusiness>;
            this._reqList = new Vector.<SBusiness>;
            Dispatcher.addEventListener(EventName.TradeRequest, this.onTradeRequest);
            Dispatcher.addEventListener(EventName.TradeOperationResult, this.onOperationResult);
            Dispatcher.addEventListener(EventName.TradeOperationError, this.onOperationError);
            Dispatcher.addEventListener(EventName.Trade_PutInItem, this.onPutInItem);
            NetDispatcher.addCmdListener(ServerCommand.TradeMsg, this.onTradeMsg);
            NetDispatcher.addCmdListener(ServerCommand.TradeApplySucess, this.onTradeApplySucess);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyAndGoodsChange);
            
        }

        public function get hintIcon() : TradeHintIcon
        {
            if (!this._tradeHintIcon)
            {
                this._tradeHintIcon = new TradeHintIcon();
                this._tradeHintIcon.addEventListener(TradeModule.Event_AcceptTrade, this.acceptHandler);
                this._tradeHintIcon.addEventListener(TradeModule.Event_RejectTrade, this.rejectHandler);
            }
            return this._tradeHintIcon;
        }

        private function onTradeApplySucess(param1:Object) : void
        {
            MsgManager.addBroadCast("已成功向对方发起交易请求");
            
        }

        override protected function initView() : IView
        {
            if (!this._tradeModule)
            {
                this._tradeModule = new TradeModule();
                this._tradeModule.addEventListener(TradeModule.Event_MoneyInputChange_gold, this.onGoldInputChange);
                this._tradeModule.addEventListener(TradeModule.Event_MoneyInputChange_coin, this.onCoinInputChange);
                this._tradeModule.addEventListener(TradeModule.Event_ItemChange, this.onChooseItemChange);
                this._tradeModule.addEventListener(TradeModule.Event_ConfirmTrade, this.onConfirmTrade);
                this._tradeModule.addEventListener(TradeModule.Event_LockTrade, this.onLockTrade);
                this._tradeModule.addEventListener(WindowEvent.CLOSE, this.closeTrade);
            }
            return this._tradeModule;
        }

        private function onTradeMsg(param1:Object = null) : void
        {
            var _loc_2:* = param1 as SBusiness;
            switch(_loc_2.operation.__value)
            {
                case EBusinessOperation._EBusinessOperationUpdateItem:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    if (this.requestFromEntityId == cache.role.entityInfo.entityId.id)
                    {
                        this._tradeModule.updateItem(_loc_2.fromInfo.items);
                    }
                    else
                    {
                        this._tradeModule.updateItem(_loc_2.toInfo.items);
                    }
                    break;
                }
                case EBusinessOperation._EBusinessOperationUpdateMoney:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    if (this.requestFromEntityId == cache.role.entityInfo.entityId.id)
                    {
                        this._tradeModule.updateMoney(_loc_2.fromInfo.gold, _loc_2.fromInfo.coin);
                    }
                    else
                    {
                        this._tradeModule.updateMoney(_loc_2.toInfo.gold, _loc_2.toInfo.coin);
                    }
                    break;
                }
                case EBusinessOperation._EBusinessOperationLock:
                {
                    this._tradeModule.lock1 = true;
                    break;
                }
                case EBusinessOperation._EBusinessOperationConfirm:
                {
                    this.onTradeComfirm(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationCancel:
                {
                    this.onTradeCancel(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationEnd:
                {
                    this.onTradeEnd(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationAgree:
                {
                    this.onAgreeToTrade(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationApply:
                {
                    this.onTradeApply(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationError:
                {
                    this.onOpertionError(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationReject:
                {
                    this.onOperationReject(_loc_2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function onMoneyAndGoodsChange(param1:Object = null) : void
        {
            if (this._tradeModule)
            {
            }
            
        }

        private function onTradeComfirm(param1:SBusiness) : void
        {
            MsgManager.addBroadCast("对方确认交易");
            
        }

        private function onTradeCancel(param1:SBusiness) : void
        {
            if (this._reqList)
            {
                if (this._reqList.length > 0 && this.currentBusinessId == this._reqList[0].businessId)
                {
                    this.currentBusinessId = null;
                }
            }
            this.view.hide();
            this.clearAtTradeEnd();
            MsgManager.addBroadCast("交易取消");
            
        }

        private function onTradeEnd(param1:SBusiness) : void
        {
            if (this._reqList)
            {
                if (this._reqList.length > 0 && this.currentBusinessId == this._reqList[0].businessId)
                {
                    this._reqList.splice(0, 1);
                    this.currentBusinessId = null;
                }
            }
            else
            {
                this.currentBusinessId = null;
            }
            this.view.hide();
            this.clearAtTradeEnd();
            MsgManager.addBroadCast("交易结束");
            
        }

        private function onOperationReject(param1:SBusiness) : void
        {
            MsgManager.addBroadCast(param1.fromInfo.name + "拒绝了你的交易请求");
            
        }

        private function onOpertionError(param1:SBusiness) : void
        {
            MsgManager.addBroadCast(ErrorCode.getErrorStringByCode(param1.error.code));
            
        }

        private function onTradeApply(param1:SBusiness) : void
        {
            var _loc_3:int = 0;
            if (SystemSetter.currentSetter.refuseTrade)
            {
                GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationReject, param1.businessId);
                
            }
            var _loc_2:Boolean = false;
            if (this.hintIcon)
            {
                this._reqList = this._tradeHintIcon._requestList;
                _loc_3 = 0;
                while (_loc_3 < this._reqList.length)
                {
                    
                    if ((this._reqList[_loc_3] as SBusiness).fromInfo.entityId.id == param1.fromInfo.entityId.id)
                    {
                        _loc_2 = true;
                        break;
                    }
                    _loc_3++;
                }
                if (!_loc_2)
                {
                    this._reqList.push(param1);
                }
                this.showHint();
            }
            
        }

        private function showHint() : void
        {
            if (this._reqList.length > 0)
            {
                UiHintIconView.instance.addHintIcon(this._tradeHintIcon);
                this.hintIcon.showHint();
                this.currentBusinessId = this._reqList[0].businessId;
            }
            else
            {
                this.currentBusinessId = null;
            }
            
        }

        private function acceptHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SBusiness;
            if (!_loc_2)
            {
                
            }
            GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationAgree, _loc_2.businessId);
            
        }

        private function rejectHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SBusiness;
            GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationReject, _loc_2.businessId);
            
        }

        private function clearAtTradeEnd() : void
        {
            cache.trade.isTrading = false;
            cache.trade.usedItems = new Dictionary();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            
        }

        private function onTradeRequest(event:DataEvent) : void
        {
            this.requestForTrade(event.data as SEntityId);
            
        }

        private function onOperationResult(param1:Object) : void
        {
            var _loc_2:* = param1.data.oper as EBusinessOperation;
            switch(_loc_2.value())
            {
                case EBusinessOperation._EBusinessOperationCancel:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_CancelSuccess));
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function onOperationError(event:DataEvent) : void
        {
            var _loc_2:* = event.data.oper as EBusinessOperation;
            var _loc_3:* = event.data.msg as Exception;
            switch(_loc_2.value())
            {
                case EBusinessOperation._EBusinessOperationConfirm:
                {
                    this._tradeModule.onComfirmError();
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function closeTrade(event:WindowEvent) : void
        {
            if (this.currentBusinessId != null)
            {
                GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationCancel, this.currentBusinessId);
                this.clearAtTradeEnd();
            }
            
        }

        private function onAgreeToTrade(param1:SBusiness) : void
        {
            this.initView();
            this.requestFromEntityId = param1.fromEntity.id;
            this.currentBusinessId = param1.businessId;
            if (this.requestFromEntityId == cache.role.entityInfo.entityId.id)
            {
                this._tradeModule.startTrade(cache.role.playerInfo.name, cache.role.entityInfo.level, param1.fromInfo.name, param1.fromInfo.level);
            }
            else
            {
                this._tradeModule.startTrade(cache.role.playerInfo.name, cache.role.entityInfo.level, param1.toInfo.name, param1.toInfo.level);
            }
            cache.trade.isTrading = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            
        }

        private function onConfirmTrade(event:DataEvent) : void
        {
            GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationConfirm, this.currentBusinessId);
            
        }

        private function onLockTrade(event:DataEvent) : void
        {
            GameProxy.tradeProxy.doOperation(EBusinessOperation.EBusinessOperationLock, this.currentBusinessId);
            
        }

        private function onChooseItemChange(event:DataEvent) : void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var _loc_2:String = "";
            if (event.data.itemData is SPetInfo || event.data.itemData is ItemData)
            {
                _loc_2 = event.data.itemData.uid;
                cache.trade.usedItems[_loc_2] = event.data.amount;
            }
            else
            {
                _loc_2 = event.data.itemData.itemData.uid;
                cache.trade.usedItems[_loc_2] = event.data.amount;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            GameProxy.tradeProxy.updateItem(this.currentBusinessId, _loc_2, event.data.amount);
            
        }

        private function onGoldInputChange(event:DataEvent) : void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var _loc_2:* = Number(event.data);
            if (_loc_2 > cache.role.money.gold)
            {
                MsgManager.addBroadCast("元宝不足");
                this._tradeModule.setGoldNum(cache.role.money.gold);
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit.EPriceUnitGold, cache.role.money.gold);
            }
            else
            {
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit.EPriceUnitGold, _loc_2);
            }
            
        }

        private function onCoinInputChange(event:DataEvent) : void
        {
            this._tradeModule.lock1 = false;
            this._tradeModule.lock2 = false;
            var _loc_2:* = Number(event.data);
            if (_loc_2 > cache.role.money.coin)
            {
                MsgManager.addBroadCast("铜钱不足");
                this._tradeModule.setCoinNum(cache.role.money.coin);
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit.EPriceUnitCoin, cache.role.money.coin);
            }
            else
            {
                GameProxy.tradeProxy.updateMoney(this.currentBusinessId, EPrictUnit.EPriceUnitCoin, Number(event.data));
            }
            
        }

        private function onPutInItem(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (ItemsUtil.isBind(_loc_2))
            {
                
            }
            this._tradeModule.putIn(event.data as ItemData);
            
        }

        public function requestForTrade(param1:SEntityId) : void
        {
            GameProxy.tradeProxy.doApply(param1);
            
        }

        public function putInPet(param1) : void
        {
            if (this._tradeModule)
            {
                this._tradeModule.putInPet(param1.itemData);
            }
            
        }

    }
}
